package com.example.myletcodelearing.one

import android.util.Log
import java.lang.StringBuilder
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.math.abs


/**
 * @author tgw
 * @date 2022/11/7
 * https://leetcode.cn/problems/letter-combinations-of-a-phone-number/comments/
输入：digits = "23"
输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]


 */

fun main(args: Array<String>) {
    var op = Solution23.Solution()
    var num = "23"
    var result = op.letterCombinations(num)


    print("打印：$result")


}

private class Solution23 {
    internal class Solution {
        fun letterCombinations(digits: String): List<String> {

            var list = ArrayList<String>()
            if (digits.length <= 0) {
                return list
            }
            //存储模板
            var num: Array<Array<Char>> = arrayOf(
                arrayOf('a', 'b', 'c'),
                arrayOf('d', 'e', 'f'),
                arrayOf('g', 'h', 'i'),
                arrayOf('j', 'k', 'l'),
                arrayOf('m', 'n', 'o'),
                arrayOf('p', 'q', 'r', 's'),
                arrayOf('t', 'u', 'v'),
                arrayOf('w', 'x', 'y', 'z'),
            )
            //计算按键 数字
            var dd = IntArray(digits.length)
            digits.forEachIndexed { index, value ->
                dd[index] = Integer.valueOf(value + "")
            }
            var n = 0
            //dd.size层for循环，拼接字符串
            forValue(n, dd, num, "", list)

            return list
        }

        fun forValue(
            n: Int,
            dd: IntArray,
            num: Array<Array<Char>>,
            value1: String,
            list: ArrayList<String>
        ): String {
            var value1 = value1
            //递归至底层，存储当前的一条字符串
            if (n == dd.size) {
                println("forValue-----:$value1")
                list.add(value1)
                return value1
            }
            //dd[n] - 2，取正确的模板值
            num[dd[n] - 2].forEachIndexed { index, value ->
                //n + 1 递归for层级+1    -------- value1 + value 拼接字符串
                forValue(n + 1, dd, num, value1 + value, list)
            }
            return ""
        }


    }


}
